﻿@model IEnumerable<TimeReporting.Models.Chat>

@{
    ViewBag.Title = "Chat";
}

<div id="updates">
<table>
    <tr>
        <th>
            User
        </th>
        <th>
            Message
        </th>
        <th>
            Time
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.userName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.chatMsg)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.timeStamp)
            </td>
            <td>
                @Html.ActionLink("Delete", "Delete", new { id = item.chatId })
            </td>
        </tr>
    }
</table>
</div>
<form action="/chat/Create" method="post" id="send">
    <input type="text" id="msgbox" />
    <input type="submit" value="Send Me" />
</form>
<script type="text/javascript">
    function update() {
        $.ajax({
            url: "/chat/chatUpdate/",
            type: 'GET',
            success: function (r) {
                $("div#updates").html(r)
            },
            error: function () {
              alert("error is here.... ");
            }
        });
        window.setTimeout("update()", 10000);
    }
    $(document).ready(function () {
        $("form#send").submit(function (e) {
            e.preventDefault();
            $.ajax({
                url: "/chat/Create",
                type: 'POST',
                data: {chatMsg: $("input#msgbox").val()},
                success: function (r) {
                    update();
                    $("input#msgbox").val("");
                },
            });
            return false;
        });
        update();
    })
</script>
